package com.amsp.oa.oademo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.amsp.oa.oademo.entity.FixedAssetsStatistics;
import com.amsp.oa.oademo.entity.FixedAssetsStatisticsDetail;
import com.amsp.oa.oademo.mapper.FixedAssetsStatisticsMapper;
import com.amsp.oa.oademo.mapper.FixedAssetsStatisticsDetailMapper;
import com.amsp.oa.oademo.service.FixedAssetsStatisticsService;
import com.amsp.oa.oademo.vo.FixedAssetsStatisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class FixedAssetsStatisticsServiceImpl extends ServiceImpl<FixedAssetsStatisticsMapper, FixedAssetsStatistics>
        implements FixedAssetsStatisticsService {

    @Autowired
    private FixedAssetsStatisticsDetailMapper detailMapper;

    @Override
    @Transactional
    public boolean saveStatistics(FixedAssetsStatisticsVO statisticsVO) {
        // 保存主表数据
        FixedAssetsStatistics statistics = new FixedAssetsStatistics();
        statistics.setMonthDate(statisticsVO.getMonth());
        statistics.setStatisticsYear(statisticsVO.getStatisticsDate().getYear());
        statistics.setStatisticsMonth(statisticsVO.getStatisticsDate().getMonth());
        statistics.setStatisticsDay(statisticsVO.getStatisticsDate().getDay());
        statistics.setReviewer(statisticsVO.getReviewer());
        statistics.setApprover(statisticsVO.getApprover());
        statistics.setCreator(statisticsVO.getCreator());

        boolean success = this.save(statistics);
        if (success && statisticsVO.getAssetList() != null) {
            // 保存明细数据
            List<FixedAssetsStatisticsDetail> details = statisticsVO.getAssetList();
            details.forEach(detail -> detail.setStatisticsId(statistics.getId()));
            details.forEach(detailMapper::insert);
        }
        return success;
    }

    @Override
    public IPage<FixedAssetsStatistics> getPage(Integer current, Integer size, String monthDate) {
        Page<FixedAssetsStatistics> page = new Page<>(current, size);
        LambdaQueryWrapper<FixedAssetsStatistics> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.hasText(monthDate)) {
            wrapper.like(FixedAssetsStatistics::getMonthDate, monthDate);
        }

        wrapper.orderByDesc(FixedAssetsStatistics::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public FixedAssetsStatisticsVO getDetailById(Long id) {
        // 获取主表数据
        FixedAssetsStatistics statistics = this.getById(id);
        if (statistics == null) {
            return null;
        }

        // 构建返回对象
        FixedAssetsStatisticsVO vo = new FixedAssetsStatisticsVO();
        vo.setMonth(statistics.getMonthDate());

        FixedAssetsStatisticsVO.StatisticsDate statisticsDate = new FixedAssetsStatisticsVO.StatisticsDate();
        statisticsDate.setYear(statistics.getStatisticsYear());
        statisticsDate.setMonth(statistics.getStatisticsMonth());
        statisticsDate.setDay(statistics.getStatisticsDay());
        vo.setStatisticsDate(statisticsDate);

        vo.setReviewer(statistics.getReviewer());
        vo.setApprover(statistics.getApprover());
        vo.setCreator(statistics.getCreator());

        // 获取明细数据
        LambdaQueryWrapper<FixedAssetsStatisticsDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FixedAssetsStatisticsDetail::getStatisticsId, id);
        List<FixedAssetsStatisticsDetail> details = detailMapper.selectList(wrapper);
        vo.setAssetList(details);

        return vo;
    }

    @Override
    @Transactional
    public boolean deleteWithDetails(Long id) {
        // 删除明细数据
        LambdaQueryWrapper<FixedAssetsStatisticsDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FixedAssetsStatisticsDetail::getStatisticsId, id);
        detailMapper.delete(wrapper);

        // 删除主表数据
        return this.removeById(id);
    }
}